<html>
	<head>
		<title>bullet demo by qq.d.y</title>
		<script type="text/javascript" src="lib.js"></script>
		<script>
			var gl, W, H, g3d;
			var PISTOL=0, ARTILLERY=1, FIREBALL=2, LASER=3, type=ARTILLERY;
			var parray = [];
			function init() {
				W = document.getElementById('canvas').width;
				H = document.getElementById('canvas').height;
				document.getElementById('btp').value = 'ARTILLERY';
				
				var cv = document.getElementById('canvas');
				gl = cv.getContext('experimental-webgl');
				if (!gl)   alert('please do not use fuck IE...');
				
				g3d = new G3D(gl, W, H, [0.7, 0.7, 1.0]);
				g3d.lookat([-25.0, 8.0, 5.0], [0.0, 5.0, 22.0], [0,1,0]);
				setInterval('render();',30);
			}
			function render() {
				gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
				gl.viewport(0, 0, W, H);
				
				g3d.line_begin();
				for (var i=0; i<200; i+=10)   g3d.line_add([-5,0,i], [5,0,i]);
				g3d.line_end([0.4,0.4,0.4]);
				
				for (var i in parray) {
					var pos = parray[i].get_position();
					if (pos[2]>200 || pos[1]<0 || pos[1]>30)   continue;
					parray[i].integrate(0.03);
					
					g3d.sphere(pos, [1,1,1], [0,0,0], [0,0,0]);
					g3d.sphere([pos[0], 0, pos[2]], [1, 0.1, 1], [0,0,0], [0.5, 0.5, 0.5]);
				}
			}
			document.onmousedown = function(e) {
				var p = new Particle();
				
				switch (type) {
				case PISTOL:
					p.set_mass(2);
					p.set_velocity(v3gen(0,0,35));
					p.set_acceleration(v3gen(0,-1,0));
					p.set_damping(0.99);
					break;
				case ARTILLERY:
					p.set_mass(200);
					p.set_velocity(v3gen(0,30,40));
					p.set_acceleration(v3gen(0,-20,0));
					p.set_damping(0.99);
					break;
				case FIREBALL:
					p.set_mass(1);
					p.set_velocity(v3gen(0,0,10));
					p.set_acceleration(v3gen(0,0.6,0));
					p.set_damping(0.9);
					break;
				case LASER:
					p.set_mass(0.1);
					p.set_velocity(v3gen(0,0,100));
					p.set_acceleration(v3gen(0,0,0));
					p.set_damping(0.99);
					break;
				}
				p.set_position(v3gen(0, 1.5, 0));
				parray.push(p);
			}
			document.onkeydown = function(e) {
				k = String.fromCharCode(e.which);
				if (k == '1') {
					type = PISTOL;
					document.getElementById('btp').value = 'PISTOL';
				} else if (k == '2') {
					type = ARTILLERY;
					document.getElementById('btp').value = 'ARTILLERY';
				} else if (k == '3') {
					type = FIREBALL;
					document.getElementById('btp').value = 'FIREBALL';
				} else if (k == '4') {
					type = LASER;
					document.getElementById('btp').value = 'LASER';
				}
			}
		</script>
	</head>
	<body onload='init()'>
		<canvas id="canvas" style="border:none;" width='640' height='320'></canvas>
		<br>click left mouse button to fire , click 1~4 num key to change bullet .<br/>
		<tr>
			<td>bullet type: <input type='text' id='btp' value='PISTOL' readonly/>
		</tr>
	</body>
</html> 